// pages/help/help.js
const app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    id:0,
    baseImgUrl:app.$http.baseImgUrl,
    help:{},
    userId:wx.getStorageSync('userId'),
    helpUser:{},
    height:wx.getSystemInfoSync().windowHeight,
    pageNum:1,
    total:0,
    triggered:true
  },
  //刷新
  onRefresh(){
    this.getData()
    this.setData({
       triggered:false,
       pageNum:1,
       total:0,
     })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({id:options.id})
    this.getData()
  },
  //网络请求，获取数据
  getData(){
    //调用接口获取求助内容
    app.$http.get('/app/help/'+this.data.id).then(res=>{
      this.setData({
        help:res.data,
        content:res.data.content.replaceAll('src="/images','style="height:100%;width:100%;border-radius: 5px;" mode="aspectFit" src="' + this.data.baseImgUrl)
      })
      //调用接口获取求助评论内容
      app.$http.get('/app/remark/list',{pageNum:this.data.pageNum,pageSize:2,helpId:this.data.id,status:1}).then(resp=>{
        this.setData({
          remarkList:resp.data.records,
          total:resp.data.total
        })
        //隐藏loading 提示框
        wx.hideLoading();
        //隐藏导航条加载动画
        wx.hideNavigationBarLoading();
        //停止下拉刷新
        wx.stopPullDownRefresh();
      }).catch(err=>{console.log(err)})
    }).catch(err=>{console.log(err)})
  },
  //网络请求，添加数据
  appendData(){
    app.$http.get('/app/remark/list',{pageNum:this.data.pageNum+1,pageSize:2,helpId:this.data.id,status:1}).then(resp=>{
      this.setData({
        pageNum:this.data.pageNum+1,
        remarkList:this.data.remarkList.concat(resp.data.records)
      })
    }).catch(err=>{console.log(err)})
  },
  //编辑评论内容
  toWriteRemark:function(e){
    var id = e.currentTarget.dataset.id
    wx.navigateTo({url: "/pages/index/help/writeRemark/writeRemark?id="+id})
  },
  //解决
  slove(){
    var that = this
    wx.showModal({
      title: '提示',
      content: '确认求助已解决',
      success (res) {
        if (res.confirm) {
          var temp = that.data.help
          temp.status = 1
          app.$http.put('/app/help/update',temp).then(resp=>{
            wx.switchTab({
              url: '/pages/my/my',
            })()
          }).catch(err=>{console.log(err)})
        }
      }
    })
  },
  //滑动到底部
  lower(){
    if (this.data.total > this.data.remarkList.length) {
      this.appendData()
    }
    console.log('滑动到底部')
  },
})